

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>C API &mdash; CVXOPT User&#39;s Guide</title>
  

  
  
  
  

  
  <script type="text/javascript" src="_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
        <script src="_static/jquery.js"></script>
        <script src="_static/underscore.js"></script>
        <script src="_static/doctools.js"></script>
        <script src="_static/language_data.js"></script>
    
    <script type="text/javascript" src="_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="copyright" title="Copyright" href="copyright.html" />
    <link rel="next" title="Matrix Formatting" href="printing.html" />
    <link rel="prev" title="Modeling" href="modeling.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="index.html" class="icon icon-home"> CVXOPT User's Guide
          

          
          </a>

          
            
            
              <div class="version">
                1.2.5
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="copyright.html">Copyright and License</a></li>
<li class="toctree-l1"><a class="reference internal" href="intro.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="matrices.html">Dense and Sparse Matrices</a></li>
<li class="toctree-l1"><a class="reference internal" href="blas.html">The BLAS Interface</a></li>
<li class="toctree-l1"><a class="reference internal" href="lapack.html">The LAPACK Interface</a></li>
<li class="toctree-l1"><a class="reference internal" href="fftw.html">Discrete Transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="spsolvers.html">Sparse Linear Equations</a></li>
<li class="toctree-l1"><a class="reference internal" href="coneprog.html">Cone Programming</a></li>
<li class="toctree-l1"><a class="reference internal" href="solvers.html">Nonlinear Convex Optimization</a></li>
<li class="toctree-l1"><a class="reference internal" href="modeling.html">Modeling</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">C API</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#dense-matrices">Dense Matrices</a></li>
<li class="toctree-l2"><a class="reference internal" href="#sparse-matrices">Sparse Matrices</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="printing.html">Matrix Formatting</a></li>
<li class="toctree-l1"><a class="reference external" href="http://cvxopt.org">cvxopt.org</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="index.html">CVXOPT User's Guide</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="index.html">Docs</a> &raquo;</li>
        
      <li>C API</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="c-api">
<span id="c-capi"></span><h1>C API<a class="headerlink" href="#c-api" title="Permalink to this headline">¶</a></h1>
<p>The API can be used to extend CVXOPT with interfaces to external C routines
and libraries.  A C program that creates or manipulates the dense or sparse
matrix objects defined in CVXOPT must include the <code class="xref py py-const docutils literal notranslate"><span class="pre">cvxopt.h</span></code> header
file in the <code class="xref py py-const docutils literal notranslate"><span class="pre">src</span></code> directory of the distribution.</p>
<p>Before the C API can be used in an extension module it must be initialized
by calling the macro <code class="xref c c-macro docutils literal notranslate"><span class="pre">import_cvxopt</span></code>.  As an example we show the
module initialization from the <code class="xref py py-mod docutils literal notranslate"><span class="pre">cvxopt.blas</span></code> module, which itself uses
the API:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#if PY_MAJOR_VERSION &gt;= 3</span>

<span class="k">static</span> <span class="n">PyModuleDef</span> <span class="n">blas_module</span> <span class="o">=</span> <span class="p">{</span>
    <span class="n">PyModuleDef_HEAD_INIT</span><span class="p">,</span>
    <span class="s">&quot;blas&quot;</span><span class="p">,</span>
    <span class="n">blas__doc__</span><span class="p">,</span>
    <span class="o">-</span><span class="mi">1</span><span class="p">,</span>
    <span class="n">blas_functions</span><span class="p">,</span>
    <span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span>
<span class="p">};</span>

<span class="n">PyMODINIT_FUNC</span> <span class="nf">PyInit_blas</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
  <span class="n">PyObject</span> <span class="o">*</span><span class="n">m</span><span class="p">;</span>
  <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="p">(</span><span class="n">m</span> <span class="o">=</span> <span class="n">PyModule_Create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">blas_module</span><span class="p">)))</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>
  <span class="k">if</span> <span class="p">(</span><span class="n">import_cvxopt</span><span class="p">()</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>
  <span class="k">return</span> <span class="n">m</span><span class="p">;</span>
<span class="p">}</span>

<span class="cp">#else</span>

<span class="n">PyMODINIT_FUNC</span> <span class="nf">initblas</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
  <span class="n">PyObject</span> <span class="o">*</span><span class="n">m</span><span class="p">;</span>
  <span class="n">m</span> <span class="o">=</span> <span class="n">Py_InitModule3</span><span class="p">(</span><span class="s">&quot;cvxopt.blas&quot;</span><span class="p">,</span> <span class="n">blas_functions</span><span class="p">,</span> <span class="n">blas__doc__</span><span class="p">);</span>
  <span class="k">if</span> <span class="p">(</span><span class="n">import_cvxopt</span><span class="p">()</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span> <span class="p">;</span>
<span class="p">}</span>

<span class="cp">#endif</span>
</pre></div>
</div>
<div class="section" id="dense-matrices">
<h2>Dense Matrices<a class="headerlink" href="#dense-matrices" title="Permalink to this headline">¶</a></h2>
<p>As can be seen from the header file <code class="xref py py-const docutils literal notranslate"><span class="pre">cvxopt.h</span></code>, a <code class="xref py py-class docutils literal notranslate"><span class="pre">matrix</span></code> is
essentially a  structure with four fields.  The fields <code class="xref c c-member docutils literal notranslate"><span class="pre">nrows</span></code> and
<code class="xref c c-member docutils literal notranslate"><span class="pre">ncols</span></code> are two integers that specify the dimensions.  The
<code class="xref c c-member docutils literal notranslate"><span class="pre">id</span></code> field controls the type of the matrix and can have values
<code class="xref py py-const docutils literal notranslate"><span class="pre">DOUBLE</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">INT</span></code>, and <code class="xref py py-const docutils literal notranslate"><span class="pre">COMPLEX</span></code>.  The <code class="xref c c-member docutils literal notranslate"><span class="pre">buffer</span></code>
field is an array that contains the matrix elements stored contiguously in
column-major order.</p>
<p>The following C functions can be used to create matrices.</p>
<dl class="c function">
<dt id="c.Matrix_New">
matrix *<code class="sig-name descname">Matrix_New</code><span class="sig-paren">(</span>int <em>nrows</em>, int <em>ncols</em>, int <em>id</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Matrix_New" title="Permalink to this definition">¶</a><br /></dt>
<dd><p>Returns a <code class="xref py py-class docutils literal notranslate"><span class="pre">matrix</span></code> object of type <cite>id</cite> with <cite>nrows</cite> rows and
<cite>ncols</cite> columns.  The elements of the matrix are uninitialized.</p>
</dd></dl>

<dl class="c function">
<dt id="c.Matrix_NewFromMatrix">
matrix *<code class="sig-name descname">Matrix_NewFromMatrix</code><span class="sig-paren">(</span>matrix *<em>src</em>, int <em>id</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Matrix_NewFromMatrix" title="Permalink to this definition">¶</a><br /></dt>
<dd><p>Returns a copy of the matrix <cite>src</cite> converted to type <cite>id</cite>.  The
following type conversions are allowed:  <code class="xref py py-const docutils literal notranslate"><span class="pre">'i'</span></code> to <code class="xref py py-const docutils literal notranslate"><span class="pre">'d'</span></code>,
<code class="xref py py-const docutils literal notranslate"><span class="pre">'i'</span></code> to <code class="xref py py-const docutils literal notranslate"><span class="pre">'z'</span></code>, and <code class="xref py py-const docutils literal notranslate"><span class="pre">'d'</span></code>  to <code class="xref py py-const docutils literal notranslate"><span class="pre">'z'</span></code>.</p>
</dd></dl>

<dl class="c function">
<dt id="c.Matrix_NewFromSequence">
matrix *<code class="sig-name descname">Matrix_NewFromSequence</code><span class="sig-paren">(</span>PyListObject *<em>x</em>, int <em>id</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Matrix_NewFromSequence" title="Permalink to this definition">¶</a><br /></dt>
<dd><p>Creates a matrix of type <cite>id</cite> from the Python sequence type <cite>x</cite>. The
returned matrix has size <code class="docutils literal notranslate"><span class="pre">(len(x),</span> <span class="pre">1)</span></code>.  The size can be changed
by modifying the <code class="xref c c-member docutils literal notranslate"><span class="pre">nrows</span></code> and <code class="xref c c-member docutils literal notranslate"><span class="pre">ncols</span></code> fields of the
returned matrix.</p>
</dd></dl>

<p>To illustrate the creation and manipulation of dense matrices (as well as
the Python C API), we show the code for the <a class="reference internal" href="matrices.html#cvxopt.uniform" title="cvxopt.uniform"><code class="xref py py-func docutils literal notranslate"><span class="pre">cvxopt.uniform</span></code></a> function
described in the section <a class="reference internal" href="matrices.html#s-random"><span class="std std-ref">Randomly Generated Matrices</span></a>.</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyObject</span> <span class="o">*</span> <span class="nf">uniform</span><span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">kwrds</span><span class="p">)</span>
<span class="p">{</span>
  <span class="n">matrix</span> <span class="o">*</span><span class="n">obj</span><span class="p">;</span>
  <span class="kt">int</span> <span class="n">i</span><span class="p">,</span> <span class="n">nrows</span><span class="p">,</span> <span class="n">ncols</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
  <span class="kt">double</span> <span class="n">a</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
  <span class="kt">char</span> <span class="o">*</span><span class="n">kwlist</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;nrows&quot;</span><span class="p">,</span> <span class="s">&quot;ncols&quot;</span><span class="p">,</span> <span class="s">&quot;a&quot;</span><span class="p">,</span> <span class="s">&quot;b&quot;</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">};</span>

  <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">PyArg_ParseTupleAndKeywords</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">kwrds</span><span class="p">,</span> <span class="s">&quot;i|idd&quot;</span><span class="p">,</span> <span class="n">kwlist</span><span class="p">,</span>
          <span class="o">&amp;</span><span class="n">nrows</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">ncols</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">b</span><span class="p">))</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>

  <span class="k">if</span> <span class="p">((</span><span class="n">nrows</span><span class="o">&lt;</span><span class="mi">0</span><span class="p">)</span> <span class="o">||</span> <span class="p">(</span><span class="n">ncols</span><span class="o">&lt;</span><span class="mi">0</span><span class="p">))</span> <span class="p">{</span>
    <span class="n">PyErr_SetString</span><span class="p">(</span><span class="n">PyExc_TypeError</span><span class="p">,</span> <span class="s">&quot;dimensions must be non-negative&quot;</span><span class="p">);</span>
    <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>
  <span class="p">}</span>

  <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="p">(</span><span class="n">obj</span> <span class="o">=</span> <span class="n">Matrix_New</span><span class="p">(</span><span class="n">nrows</span><span class="p">,</span> <span class="n">ncols</span><span class="p">,</span> <span class="n">DOUBLE</span><span class="p">)))</span>
    <span class="k">return</span> <span class="n">PyErr_NoMemory</span><span class="p">();</span>

  <span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">nrows</span><span class="o">*</span><span class="n">ncols</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
    <span class="n">MAT_BUFD</span><span class="p">(</span><span class="n">obj</span><span class="p">)[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">Uniform</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">);</span>

  <span class="k">return</span> <span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">)</span><span class="n">obj</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="sparse-matrices">
<h2>Sparse Matrices<a class="headerlink" href="#sparse-matrices" title="Permalink to this headline">¶</a></h2>
<p>Sparse matrices are stored in compressed column storage (CCS) format.  For
a general <cite>nrows</cite> by <cite>ncols</cite> sparse matrix with <cite>nnz</cite> nonzero entries this
means the following.  The sparsity pattern and the nonzero values are
stored in three fields:</p>
<dl class="simple">
<dt><code class="xref c c-member docutils literal notranslate"><span class="pre">values</span></code></dt><dd><p>An array of floating-point numbers of length <cite>nnz</cite> with the
nonzero entries of the matrix stored columnwise.</p>
</dd>
<dt><code class="xref c c-member docutils literal notranslate"><span class="pre">rowind</span></code></dt><dd><p>An array of integers of length <cite>nnz</cite> containing the row indices of
the nonzero entries, stored in the same order as <code class="xref c c-member docutils literal notranslate"><span class="pre">values</span></code>.</p>
</dd>
<dt><code class="xref c c-member docutils literal notranslate"><span class="pre">colptr</span></code></dt><dd><p>An array of integers of length <cite>ncols</cite> + 1 with for each column of the
matrix the index of the first element in <code class="xref c c-member docutils literal notranslate"><span class="pre">values</span></code> from that
column.  More precisely, <code class="docutils literal notranslate"><span class="pre">colptr[0]</span></code> is <code class="xref py py-const docutils literal notranslate"><span class="pre">0</span></code>, and for
k = 0, 1, …, <cite>ncols</cite> - 1, <code class="docutils literal notranslate"><span class="pre">colptr[k+1]</span></code> is equal to
<code class="docutils literal notranslate"><span class="pre">colptr[k]</span></code> plus the number of nonzeros in column <cite>k</cite> of the
matrix.  Thus, <code class="docutils literal notranslate"><span class="pre">colptr[ncols]</span></code> is equal to <cite>nnz</cite>, the number of
nonzero entries.</p>
</dd>
</dl>
<p>For example, for the matrix</p>
<div class="math">
<p><img src="_images/math/9173aacc17760942bd0ce61e5ad70788a84297d5.png" alt="A=\left [\begin{array}{cccc}
    1 &amp; 0 &amp; 0 &amp; 5\\
    2 &amp; 0 &amp; 4 &amp; 0\\
    0 &amp; 0 &amp; 0 &amp; 6\\
    3 &amp; 0 &amp; 0 &amp; 0
\end{array}\right]"/></p>
</div><p>the elements of <code class="xref c c-member docutils literal notranslate"><span class="pre">values</span></code>, <code class="xref c c-member docutils literal notranslate"><span class="pre">rowind</span></code>, and <code class="xref c c-member docutils literal notranslate"><span class="pre">colptr</span></code>
are:</p>
<dl class="simple">
<dt><code class="xref c c-member docutils literal notranslate"><span class="pre">values</span></code>:</dt><dd><p>1.0, 2.0, 3.0, 4.0, 5.0, 6.0</p>
</dd>
<dt><code class="xref c c-member docutils literal notranslate"><span class="pre">rowind</span></code>:</dt><dd><p>0, 1,3, 1, 0, 2</p>
</dd>
<dt><code class="xref c c-member docutils literal notranslate"><span class="pre">colptr</span></code>:</dt><dd><p>0, 3, 3, 4, 6.</p>
</dd>
</dl>
<p>It is crucial that for each column the row indices in <code class="xref c c-member docutils literal notranslate"><span class="pre">rowind</span></code> are
sorted; the equivalent representation</p>
<dl class="simple">
<dt><code class="xref c c-member docutils literal notranslate"><span class="pre">values</span></code>:</dt><dd><p>3.0, 2.0, 1.0, 4.0, 5.0, 6.0</p>
</dd>
<dt><code class="xref c c-member docutils literal notranslate"><span class="pre">rowind</span></code>:</dt><dd><p>3, 1, 0, 1, 0, 2</p>
</dd>
<dt><code class="xref c c-member docutils literal notranslate"><span class="pre">colptr</span></code>:</dt><dd><p>0, 3, 3, 4, 6</p>
</dd>
</dl>
<p>is not allowed (and will likely cause the program to crash).</p>
<p>The <code class="xref c c-member docutils literal notranslate"><span class="pre">nzmax</span></code> field specifies the number of non-zero elements the
matrix can store.  It is equal to the length of <code class="xref c c-member docutils literal notranslate"><span class="pre">rowind</span></code> and
<code class="xref c c-member docutils literal notranslate"><span class="pre">values</span></code>; this number can be larger that <code class="docutils literal notranslate"><span class="pre">colptr[nrows]</span></code>,
but never less.  This field makes it possible to preallocate a certain
amount of memory to avoid reallocations if the matrix is constructed
sequentially by filling in elements.  In general the <code class="xref c c-member docutils literal notranslate"><span class="pre">nzmax</span></code> field
can safely be ignored, however, since it will always be adjusted
automatically as the number of non-zero elements grows.</p>
<p>The <code class="xref c c-member docutils literal notranslate"><span class="pre">id</span></code> field controls the type of the matrix and can have
values <code class="xref py py-const docutils literal notranslate"><span class="pre">DOUBLE</span></code> and <code class="xref py py-const docutils literal notranslate"><span class="pre">COMPLEX</span></code>.</p>
<p>Sparse matrices are created using the following functions from the API.</p>
<dl class="c function">
<dt id="c.SpMatrix_New">
spmatrix *<code class="sig-name descname">SpMatrix_New</code><span class="sig-paren">(</span>int_t <em>nrows</em>, int_t <em>ncols</em>, int_t <em>nzmax</em>, int <em>id</em><span class="sig-paren">)</span><a class="headerlink" href="#c.SpMatrix_New" title="Permalink to this definition">¶</a><br /></dt>
<dd><p>Returns a sparse zero matrix with <cite>nrows</cite> rows and <cite>ncols</cite> columns.
<cite>nzmax</cite> is the number of elements that will be allocated (the length of
the <code class="xref c c-member docutils literal notranslate"><span class="pre">values</span></code> and <code class="xref c c-member docutils literal notranslate"><span class="pre">rowind</span></code> fields).</p>
</dd></dl>

<dl class="c function">
<dt id="c.SpMatrix_NewFromMatrix">
spmatrix *<code class="sig-name descname">SpMatrix_NewFromMatrix</code><span class="sig-paren">(</span>spmatrix *<em>src</em>, int <em>id</em><span class="sig-paren">)</span><a class="headerlink" href="#c.SpMatrix_NewFromMatrix" title="Permalink to this definition">¶</a><br /></dt>
<dd><p>Returns a copy the sparse matrix var{src}.</p>
</dd></dl>

<dl class="c function">
<dt id="c.SpMatrix_NewFromIJV">
spmatrix *<code class="sig-name descname">SpMatrix_NewFromIJV</code><span class="sig-paren">(</span>matrix *<em>I</em>, matrix *<em>J</em>, matrix *<em>V</em>, int_t <em>nrows</em>, int_t <em>ncols</em>, int <em>id</em><span class="sig-paren">)</span><a class="headerlink" href="#c.SpMatrix_NewFromIJV" title="Permalink to this definition">¶</a><br /></dt>
<dd><p>Creates a sparse matrix with <cite>nrows</cite> rows and <cite>ncols</cite> columns from a
triplet description.  <cite>I</cite> and <cite>J</cite> must be integer matrices and <cite>V</cite>
either a double or complex matrix, or <code class="xref py py-const docutils literal notranslate"><span class="pre">NULL</span></code>. If <cite>V</cite> is
<code class="xref py py-const docutils literal notranslate"><span class="pre">NULL</span></code> the values of the entries in the matrix are undefined,
otherwise they are specified by <cite>V</cite>.  Repeated entries in <cite>V</cite> are
summed.  The number of allocated elements is given by <cite>nzmax</cite>, which is
adjusted if it is smaller than the required amount.</p>
</dd></dl>

<p>We illustrate use of the sparse matrix class by listing the source
code for the <a class="reference internal" href="matrices.html#real" title="real"><code class="xref py py-attr docutils literal notranslate"><span class="pre">real</span></code></a> method, which returns the real part of
a sparse matrix:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span> <span class="nf">spmatrix_real</span><span class="p">(</span><span class="n">spmatrix</span> <span class="o">*</span><span class="n">self</span><span class="p">)</span> <span class="p">{</span>

  <span class="k">if</span> <span class="p">(</span><span class="n">SP_ID</span><span class="p">(</span><span class="n">self</span><span class="p">)</span> <span class="o">!=</span> <span class="n">COMPLEX</span><span class="p">)</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">)</span><span class="n">SpMatrix_NewFromMatrix</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">SP_ID</span><span class="p">(</span><span class="n">self</span><span class="p">));</span>

  <span class="n">spmatrix</span> <span class="o">*</span><span class="n">ret</span> <span class="o">=</span> <span class="n">SpMatrix_New</span><span class="p">(</span><span class="n">SP_NROWS</span><span class="p">(</span><span class="n">self</span><span class="p">),</span> <span class="n">SP_NCOLS</span><span class="p">(</span><span class="n">self</span><span class="p">),</span>
      <span class="n">SP_NNZ</span><span class="p">(</span><span class="n">self</span><span class="p">),</span> <span class="n">DOUBLE</span><span class="p">);</span>
  <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">ret</span><span class="p">)</span> <span class="k">return</span> <span class="n">PyErr_NoMemory</span><span class="p">();</span>

  <span class="kt">int</span> <span class="n">i</span><span class="p">;</span>
  <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">SP_NNZ</span><span class="p">(</span><span class="n">self</span><span class="p">);</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
    <span class="n">SP_VALD</span><span class="p">(</span><span class="n">ret</span><span class="p">)[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">creal</span><span class="p">(</span><span class="n">SP_VALZ</span><span class="p">(</span><span class="n">self</span><span class="p">)[</span><span class="n">i</span><span class="p">]);</span>

  <span class="n">memcpy</span><span class="p">(</span><span class="n">SP_COL</span><span class="p">(</span><span class="n">ret</span><span class="p">),</span> <span class="n">SP_COL</span><span class="p">(</span><span class="n">self</span><span class="p">),</span> <span class="p">(</span><span class="n">SP_NCOLS</span><span class="p">(</span><span class="n">self</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="k">sizeof</span><span class="p">(</span><span class="n">int_t</span><span class="p">));</span>
  <span class="n">memcpy</span><span class="p">(</span><span class="n">SP_ROW</span><span class="p">(</span><span class="n">ret</span><span class="p">),</span> <span class="n">SP_ROW</span><span class="p">(</span><span class="n">self</span><span class="p">),</span> <span class="n">SP_NNZ</span><span class="p">(</span><span class="n">self</span><span class="p">)</span><span class="o">*</span><span class="k">sizeof</span><span class="p">(</span><span class="n">int_t</span><span class="p">));</span>
  <span class="k">return</span> <span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">)</span><span class="n">ret</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
</div>


           </div>
           
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="printing.html" class="btn btn-neutral float-right" title="Matrix Formatting" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="modeling.html" class="btn btn-neutral float-left" title="Modeling" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; <a href="copyright.html">Copyright</a> 2004-2020, M.S. Andersen, J. Dahl, L. Vandenberghe
      <span class="lastupdated">
        Last updated on Apr 16, 2020.
      </span>

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
  
    <div class="footer">
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 3.0.1.
    </div>

</body>
</html>